@functions{
    dynamic GetLocation(dynamic deal) {        
        IEnumerable<dynamic> options = deal.options;
        var option = options.FirstOrDefault(o => o.redemptionLocations.Length > 0);
        if (option == null) {
            return new { lat = deal.division.lat, lng = deal.division.lng };
		} else {
            return new { lat = option.redemptionLocations[0].lat, lng = option.redemptionLocations[0].lng };
		}        
	}
}
@{
    var divisionId = UrlData[0];    
    var deals = Groupon.GetDeals(divisionId: divisionId, show: "all");
    var dealsList =  deals.Select(d => new { 
                                    division = new { lat = d.division.lat, lng = d.division.lng },
                                    title = d.announcementTitle,
                                    details = d.Title ??"",
                                    location = GetLocation(d),									
									imageURL = d.sidebarImageURL ?? "",
									dealURL = d.dealURL ?? ""
                                  });

    Response.ClearContent();
    Response.AppendHeader("Content-Type", "application/json");
    Response.Write(Json.Encode(dealsList));
    Response.End();    
}
